/*
 * CircularQueue.cpp
 *
 *  Created on: 2 mars 2011
 *      Author: eric
 */

#include "CircularQueue.h"

CircularQueue::CircularQueue() {
	// size of the queue
	q.size = 16;

	// init the header and tail pointer
	q.head = 0;
	q.length = 0;
}

void CircularQueue::push(Can::CAN_MSG canMsg) {
	// make sure the buffer has room
	if (q.length < q.size) {
		// save data byte at end of buffer
		q.canBuf[(q.head + q.length) % q.size]=canMsg;

		// increment the length
		q.length++;
	}
}

Can::CAN_MSG CircularQueue::pop() {
	Can::CAN_MSG canMsg;
	if (q.length) {
		// get the first character from buffer
		canMsg=q.canBuf[q.head];

		q.head++;

		// once the head reach the end of the buffer
		// then move it to the front
		if (q.head >= q.size)
			q.head %= q.size;

		// decrease the count of the data
		q.length--;
	}
	return canMsg;
}

Can::CAN_MSG CircularQueue::peek() {
	Can::CAN_MSG canMsg;
	if (q.length) {
		canMsg =q.canBuf[q.head];
	}
	return canMsg;
}

uint8_t CircularQueue::count() {
	return q.length;
}

bool CircularQueue::isEmpty() {
	return (q.length==0);
}

bool CircularQueue::isFull() {
	return (q.length==q.size);
}
